﻿namespace PhotobucketNet
{
    using System;
    using System.Net;
    using System.Xml;

    public class PhotobucketException : Exception
    {
        protected PhotobucketNet.ErrorCode _errorCode;
        internal XmlResponseMessage _response;
        protected string _responseMessage;

        public PhotobucketException(string message) : base(message)
        {
            this._responseMessage = string.Empty;
            this._errorCode = PhotobucketNet.ErrorCode.None;
        }

        public PhotobucketException(PhotobucketNet.ErrorCode code, string message) : base(message)
        {
            this._responseMessage = string.Empty;
            this._errorCode = PhotobucketNet.ErrorCode.None;
            this._errorCode = code;
        }

        public PhotobucketException(string message, Exception innerException) : base(message, innerException)
        {
            this._responseMessage = string.Empty;
            this._errorCode = PhotobucketNet.ErrorCode.None;
        }

        internal static PhotobucketException CreateFromWebException(WebException webEx)
        {
            if ((!webEx.Message.Contains("401") && !webEx.Message.Contains("500")) || !(webEx.Response.ContentType == "text/xml"))
            {
                if (webEx.Message.Contains("404"))
                {
                    PhotobucketApiException exception2 = new PhotobucketApiException(webEx);
                    exception2._errorCode = PhotobucketNet.ErrorCode.CouldNotGet;
                    return exception2;
                }
                return new PhotobucketWebException(webEx);
            }
            PhotobucketApiException exception = new PhotobucketApiException(webEx);
            exception._response = new XmlResponseMessage(webEx.Response.GetResponseStream());
            XmlNode node = exception._response.ResponseXml.SelectSingleNode("descendant::message");
            exception._responseMessage = node.InnerText;
            switch (Convert.ToInt32(exception._response.ResponseXml.SelectSingleNode("descendant::code").InnerText))
            {
                case 7:
                case 9:
                case 0x6c:
                case 0x6d:
                case 0x6f:
                case 0x8a:
                    exception._errorCode = PhotobucketNet.ErrorCode.InvalidPermissions;
                    return exception;

                case 0x66:
                case 0x7b:
                case 0x8b:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotGet;
                    return exception;

                case 0x67:
                case 0x68:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotSearch;
                    return exception;

                case 0x69:
                case 0x6a:
                case 0x6b:
                case 110:
                case 0x70:
                case 0x71:
                case 0x79:
                case 0x7c:
                case 0x7d:
                case 0x7e:
                case 130:
                case 0x89:
                case 140:
                case 0x8d:
                case 0x8e:
                case 0x8f:
                case 0x90:
                case 0x91:
                case 0x92:
                case 0x93:
                case 0x94:
                case 0x95:
                case 0x98:
                case 0x99:
                case 0x9a:
                case 0x9f:
                case 0xcb:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotUploadOrUpdate;
                    return exception;

                case 0x73:
                case 0x74:
                case 0x75:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotCreate;
                    return exception;

                case 0x76:
                case 0xcc:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotEmail;
                    return exception;

                case 0x83:
                case 0x84:
                case 0x85:
                case 0x86:
                case 0x87:
                case 0x88:
                    exception._errorCode = PhotobucketNet.ErrorCode.CouldNotAdd;
                    return exception;
            }
            exception._errorCode = PhotobucketNet.ErrorCode.UnknownError;
            return exception;
        }

        public PhotobucketNet.ErrorCode ErrorCode
        {
            get
            {
                return this._errorCode;
            }
        }

        internal XmlResponseMessage Response
        {
            get
            {
                return this._response;
            }
        }

        public string ResponseMessage
        {
            get
            {
                return this._responseMessage;
            }
        }
    }
}

